The Shuffler's Dilemma
Anyone can shuffle cards by hand; getting a computer to do it takes talent. Should the computer imitate a human shuffler or behave like a machine? How can the results of a shuffle be evaluated? This case study describes programs implementing several different shuffling algorithms and compares the results.
Problem Statement
Write a function called Shuffle to rearrange a (simulated) deck of cards. Create a function that will rearrange the cards such that any ordering of the cards is equally probable. Shuffle will be used in programs that simulate the play of card games and that compare different shuffling algorithms.
The function will have the following header:
def Shuffle(deck):
It will assume the following definitions:
DECKSIZE = 52
CardType = some type
DeckType = [0 for _ in range(DECKSIZE)]
Shuffle will rearrange the contents of deck, returning its "cards" in a different order. Each element contained in deck when Shuffle is entered will also be somewhere in deck - possibly in a different position when Shuffle returns. The function may make no assumptions about how CardType is defined, except to assume that assignments are possible between two CardType values.
Analysis
10.1 Suppose that a "deck" has only three cards. List all the ways the cards can be ordered.
Analysis
10.2 How many possible ways can N "cards" be arranged in a deck?
Analysis
10.3 How many possible rearrangements of an N-card deck change the position of all N cards?
Application
10.4 Define a type that represents all the characteristics of an actual playing card used in a game such as gin rummy.
Preparation
The reader is expected to be familiar with arrays and dictionary.
Understanding the Problem
What does "shuffling" a simulated deck mean?
This problem involves simulating an activity that is usually done by hand. It makes sense to pinpoint the similarities and differences between shuffling by hand and "shuffling" in a program. Shuffling a deck of actual cards means mixing them up. "Shuffling" a simulated deck means the same thing. Here's an example that uses a deck of four "cards," each being a letter.
Original deck contents
deck[1] | deck[2] | deck[3] | deck[4] |
'A' | 'B' | 'C' | 'D' |
Possible result of "shuffling"
deck[1] | deck[2] | deck[3] | deck[4] |
'C' | 'B' | 'D' | 'A' |
Another possible result of "shuffling"
deck[1] | deck[2] | deck[3] | deck[4] |
'D' | 'C' | 'B' | 'A' |
Note that the result of shuffling need not move all the cards; the first shuffle above left card 'B' in the second position in the deck.
Stop & Consider
Might shuffling leave a deck unchanged? Why or why not?
Stop & Help
How many possible ways can four cards be arranged in a deck?